//
//  NSDictionary+DeleteNull.m
//

#import "NSDictionary+DeleteNull.h"

@implementation NSDictionary (DeleteNull)
#pragma mark - 私有方法
//将NSDictionary中的Null类型的项目转化成nil
+(NSDictionary *)nullDic:(NSDictionary *)myDic
{
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [NSMutableDictionary dictionaryWithDictionary:myDic];
    for (int i = 0; i < keyArr.count; i ++)
    {
        id obj = [myDic objectForKey:keyArr[i]];
        
        if([obj isKindOfClass:[NSNull class]]){
            [resDic removeObjectForKey:keyArr[i]];
        }else{
            obj = [self deleteNull:obj];
        }
    }
    return resDic;
}

//将NSDictionary中的Null类型的删除
+(NSArray *)nullArr:(NSArray *)myArr
{
    NSMutableArray *resArr = [NSMutableArray arrayWithArray:myArr];
    for (int i = 0; i < myArr.count; i ++)
    {
        id obj = myArr[i];
        
        if([obj isKindOfClass:[NSNull class]]){
            [resArr removeObject:obj];
        }else{
            obj = [self deleteNull:obj];
        }
    }
    return resArr;
}

//将NSString类型的原路返回
+(NSString *)stringToString:(NSString *)string
{
    return string;
}

//将Null类型的项目转化成nil
+(NSString *)nullToString
{
    return nil;
}

#pragma mark - 公有方法
//类型识别:将所有的NSNull类型转化成nil
+(id)deleteNull:(id)myObj
{
    if ([myObj isKindOfClass:[NSDictionary class]])
    {
        return [self nullDic:myObj];
    }
    else if([myObj isKindOfClass:[NSArray class]])
    {
        return [self nullArr:myObj];
    }
    else if([myObj isKindOfClass:[NSString class]])
    {
        return [self stringToString:myObj];
    }
    else if([myObj isKindOfClass:[NSNull class]])
    {
        return [self nullToString];
    }
    else
    {
        return myObj;
    }
}
@end
